package com.wang.demo05;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyInvocationHandler implements InvocationHandler {

    //设置需要代理的类
    private Object proxyclass;

    public void setProxyclass(Object proxyclass) {
        this.proxyclass = proxyclass;
    }
    //获取代理类
    public Object getproxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),proxyclass.getClass().getInterfaces(),this);
    }

    //返回结果
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //添加日志
        log(method.getName());
        Object result=method.invoke(proxyclass,args);
        return result;
    }
    //添加日志
    public void log(String msg){
        System.out.println("使用了"+msg+"方法");
    }
}
